package cn.yjr.vectori.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import cn.yjr.vector.R;
import cn.yjr.vector.tools.RegistrationInfo;
import cn.yjr.vector.tools.TransactionRecord;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

/**
 * @author vector
 *
 */
public class TransactionRecordAdapter extends BaseAdapter implements StickyListHeadersAdapter{

	Context context;
	LayoutInflater inflater;
	List<TransactionRecord> contents;
	public TransactionRecordAdapter(Context context) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		contents = new ArrayList<TransactionRecord>();
		initTransactionRecord();
	}

	private void addInfo(Date date){
		String name = "项目名称";
		double cost = 250.00;

		TransactionRecord tmp = new TransactionRecord(date, name, cost);
		contents.add(tmp);
		contents.add(tmp);
	}
	@SuppressLint("SimpleDateFormat")
	private void initTransactionRecord(){

		addInfo(new Date());
		addInfo(new Date());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String strDate = "2013-12-20 13:54";
		try {
			Date date = sdf.parse(strDate);
			addInfo(date);
			addInfo(date);
			addInfo(date);
			addInfo(date);
			addInfo(date);
			addInfo(date);
			addInfo(date);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Override
	public int getCount() {
		
		return contents.size();
	}

	@Override
	public Object getItem(int position) {
		return contents.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.transaction_record_item, parent,false);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name);
			holder.time_info_tv = (TextView) convertView.findViewById(R.id.time_info);
			holder.cost_tv = (TextView) convertView.findViewById(R.id.cost);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		TransactionRecord tmp = contents.get(position);
		holder.name_tv.setText(tmp.getName());
		holder.time_info_tv.setText(tmp.getMonthDay());
		holder.cost_tv.setText(tmp.getCost()+"");
		return convertView;
	}

	@Override
	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		HeaderViewHolder holder;
		if(convertView == null){
			holder = new HeaderViewHolder();
			convertView = inflater.inflate(R.layout.transaction_record_head, parent,false);
			holder.year_month_tv = (TextView) convertView.findViewById(R.id.year_month);
			holder.total_cost_tv = (TextView) convertView.findViewById(R.id.total_cost);
			convertView.setTag(holder);
			
		}else{
			holder = (HeaderViewHolder) convertView.getTag();
		}
		TransactionRecord tmp = contents.get(position);
		holder.year_month_tv.setText(tmp.getYearMonth());
		holder.total_cost_tv.setText("" + TransactionRecord.totalCostList.get(getHeaderId(position)+""));
		return convertView;
	}

	@Override
	public long getHeaderId(int position) {
		TransactionRecord tmp = contents.get(position);
		return tmp.getId();
	}

	class HeaderViewHolder {
        TextView year_month_tv;
        TextView total_cost_tv;
    }

    class ViewHolder {
        TextView name_tv;
        TextView time_info_tv;
        TextView cost_tv;
    }
	
}
